Extended Syllabus PDF
PDF - (179 - 185)
Past Team Assignments
Active Team Assignments
A function is a set of statements organized together to perform a specific task
ex: mean() (arithmetic mean)
## [1] 2
## [1] 2
Problem: Take a sample belonged to population, and sum
## [1] 1 2 3 4 5 6
## [1] 3 1
## [1] 4
I want to collect my sample() and sum() functions and put in ONE function. I will create a new function named roll2().
name_new_function <- function( argument ) {
name_new_variable <- do_this( argument, option )
then_do_this( name_new_variable )
}
name_new_function( ) # It will work with my default argument
name_new_function <- function( argument ) {
name_new_variable <- do_this( argument, option )
then_do_this( name_new_variable )
}
name_new_function( ) # It will work with my default argument
in formal :
## [1] 10
## [1] 7
You can change the default argument in every time
## [1] 11
## [1] 18
## [1] 9.5
You can add new options or new functions in your new function.
{ } and () are important
# Think about these functions
# mean(), print(), plot(), max(), install.packages(), help(), ...
TRUE & TRUE
TRUE & FALSE
TRUE | FALSE
!TRUE
2 == 3
5 < 6
c(1,4) >= 6
9 != 8
5 < 6 & 9 != 8
score <- 80
exam_name <- "math"
score >= 75 & exam_name == "math"
The if statement executes a chunk of code if and only if a defined condition is TRUE, which looks something like this:
num <- -1
if ( num < 0 ) {
print("num is negative.")
print("Don't worry, I'll fix it.")
num <- num * -1
print(num)
print("Now num is positive.")
}## [1] "num is negative."
## [1] "Don't worry, I'll fix it."
## [1] 1
## [1] "Now num is positive."
just look
just look
Try it
Is it in the range ?
x <- 6
y <- 2
if ( x > 3 & x == 7 ) {
print("It is in the range")
z <- x + y
print(z)
}
Can you fix this, with just ONE symbol ?
Is it in the range ?
## [1] "It is in the range"
## [1] 8
Try it
You have a fruit which is yellow.
Is that a banana?
Or an orange ?
banana <- "yellow"
my_fruit <- "yellow"
if ( my_fruit == banana ) {
print("Your fruit might be a banana")
}
orange <- "orange"
my_fruit <- "yellow"
if ( my_fruit == orange ) {
print("Your fruit is not a banana")
}
If you want something different to happen when the condition is FALSE, you can add an else declaration.
## but this will !
Report the weather. Is it rainy ?
Let’s say we have two conditions. Rainy or Shiny.
And you have recorded 4 mm precipitation.
weather <- # mm # fill this number
if ( ) { # fill this condition, using weather
print("it is rainy")
} else {
print("it is shiny")
}
Report the weather. Is it rainy ?
Let’s say we have two conditions. Rainy or Shiny.
And you have recorded 4 mm precipitation.
weather <- 4 # mm # fill this number
if ( weather > 0 ) { # fill this condition, using weather
print("it is rainy")
} else {
print("it is shiny")
}## [1] "it is rainy"
If your situation has more than two mutually exclusive cases, use else and if statements together.
If your situation has more than two mutually exclusive cases, use else and if statements together.
a <- 1 # team 'a' has 1 goal
b <- 1 # team 'b' has 1 goal
if (a > b) {
print("A wins!")
} else if (a < b) {
print("B wins!")
} else {
print("Tie.")
}## [1] "Tie."
What do you gonna do ? Eating, Sleeping, or Reading?
When do you want to read ?
So, start to think about your feelings about sleeping or eating to decide whether reading or not.
What do you gonna do ? Eating, Sleeping, or Reading?
Let’s start with define the conditions
If you are hungry –> eat
If you are sleepy –> sleep
What do you gonna do ? Eat, Sleep or Read?
Let’s define your feelings. Yes (TRUE,1) or No (FALSE,0)
hungry <- # TRUE or FALSE (1 or 0)
sleepy <- # TRUE or FALSE (1 or 0)
What do you gonna do ? Eat, Sleep or Read?
hungry <- TRUE # Yes , TRUE , 1
sleepy <- TRUE # Yes , TRUE , 1
if ( ) {
print(" ")
} else if ( ) {
print(" ")
} else if ( ) {
print(" ")
} else {
print(" ")
}
What do you gonna do ? Eat, Sleep or Read?
hungry <- TRUE # Yes , TRUE , 1
sleepy <- TRUE # Yes , TRUE , 1
if ( ) { # hungry is TRUE and sleepy is TRUE
print(" ")
} else if ( ) { # hungry is TRUE and sleepy is FALSE
print(" ")
} else if ( ) { # hungry is FALSE and sleepy is TRUE
print(" ")
} else { # hungry is FALSE and sleepy is FALSE
print(" ")
}
What do you gonna do ? Eat, Sleep or Read?
hungry <- TRUE # Yes , TRUE , 1
sleepy <- TRUE # Yes , TRUE , 1
if (hungry==TRUE & sleepy==TRUE) { # hungry is TRUE and sleepy is TRUE
print(" ")
} else if (hungry==TRUE & sleepy==FALSE) { # hungry is TRUE and sleepy is FALSE
print(" ")
} else if (hungry==FALSE & sleepy==TRUE) { # hungry is FALSE and sleepy is TRUE
print(" ")
} else { # hungry is FALSE and sleepy is FALSE
print(" ")
}## [1] " "
What do you gonna do ? Eat, Sleep or Read?
hungry <- TRUE # Yes , TRUE , 1
sleepy <- TRUE # Yes , TRUE , 1
if (hungry==TRUE & sleepy==TRUE) { # hungry is TRUE and sleepy is TRUE
print("EAT")
} else if (hungry==TRUE & sleepy==FALSE) { # hungry is TRUE and sleepy is FALSE
print("EAT")
} else if (hungry==FALSE & sleepy==TRUE) { # hungry is FALSE and sleepy is TRUE
print("SLEEP")
} else { # hungry is FALSE and sleepy is FALSE
print("READ")
}## [1] "EAT"
An if statement can be placed in another if statement. In the editor, modify the mynumber example once more as follows:
if (...) {
print(...)
}
if (...) {
print("Go to bed!")
} else {
print("Wake up!")
}
if (...) {
print("I print this when it is true!")
} else {
print(...)
}
ROLE PLAY : You are a CAR, and you are going on the road.
Problem : BUT ;
How can you move ?
How can you move ?
Parameters :
How can you move ?
Parameters :
You are a CAR, BUT ;
Parameters :
First, define the situation
Traffic_Stop_Light <-
Number_of_Pedestrians <-
You are a CAR, BUT ;
Parameters :
First, define the situation
You are a CAR, BUT ;
Second, define the conditions to move again
Traffic_Stop_Light : ?
Number_of_Pedestrians : ?
Situation
Condition
Traffic_Stop_Light : 'green'
Number_of_Pedestrians : 0
Remember rules
if (...) {
print(...);
} else {
print(...);
}
Situation
Condition
Traffic_Stop_Light : 'green'
Number_of_Pedestrians : 0
Remember rules
if ( & ) {
print('Go!');
} else {
print('STOP');
}
Situation
Condition
Traffic_Stop_Light : 'green'
Number_of_Pedestrians : 0
Remember rules
if (Traffic_Stop_Light=='green' & Number_of_Pedestrians==0) {
print('Go!');
} else {
print('STOP');
}## [1] "STOP"
Problem : You want to enjoy at the weekend (“Saturday” or “Sunday”), and let’s say the day is;
What do you gonna do if it is Friday.
if (...) {
print('Enjoy the weekend!')
} else {
print('Do some work.')
}
Problem : You want to enjoy at the weekend (“Saturday” or “Sunday”), and let’s say the day is;
What do you gonna do if it is Friday.
if ( day... | day... ) {
print('Enjoy the weekend!')
} else {
print('Do some work.')
}
ANSWER : You want to enjoy, and let’s say the day is;
It is okay, you can fun if it is weekend.
if (day == 'Saturday' | day == 'Sunday') {
print('Enjoy the weekend!')
} else {
print('Do some work.')
}## [1] "Do some work."
Problem : You want to go out and your question is
“Should I take an umbrella?”
Note : There are two variables in your code,
“sky” (character) and “high_chance_of_rain” (logical)
Check, if “sky” is equal to “cloudy” and, whether there is a “high_chance_of_rain”.
If both are true, the code should print: “Take umbrella!”
Otherwise, the code should print: “No need for umbrella!”
Based on the condition, what is the answer?
RADIO: The sky is cloudy and the chance of rain is high.Your conditions, for two variables
# you want to go out and your question is "Should I take an umbrella?"
sky <-
high_chance_of_rain <-
Your conditions, for two variables
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUEif (...) {
print("...")
} else {
print("...")
}
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUEif (...) {
print("Take umbrella!")
} else {
print("No need for umbrella!")
}
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUEif ( sky... & high_chance_of_rain... ) {
print("Take umbrella!")
} else {
print("No need for umbrella!")
}
# you want to go out and your question is "Should I take an umbrella?"
sky <- "cloudy"
high_chance_of_rain <- TRUE
if (sky == "cloudy" & high_chance_of_rain == TRUE) {
print("Take umbrella!")
} else {
print("No need for umbrella!")
}## [1] "Take umbrella!"
https://learn.datacamp.com/courses/intermediate-r
Loops, DataCamp
R Programming - Part - I & II
kahoot.it